<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>推荐位管理</el-breadcrumb-item>
        <el-breadcrumb-item> 书架 </el-breadcrumb-item>
        <el-breadcrumb-item> 顶部推荐 </el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag v-if="scope.row.edit_account">{{
            scope.row.edit_account
          }}</el-tag>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import {
  appConfigBannerList as baseURL,
  appPopup,
  uploadFile,
  getGroupList
} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "sort&ASC,create_time&ASC",
        position_id: 53,
      },
      option: {
        border: true,
        selection: true,
        border: true,
        align: "center",
        headerAlign: "center",
        index: true,
        indexLabel: "编号",
        column: [
          {
            label: "APP名称",
            prop: "app_id",
            type: "select",
            gutter: "1",
            value: 61096114,
            filter: false,
            hide: true,
            placeholder: "APP名称",
            dicData: [
              {
                label: "橘子猫Android(轻小说)",
                value: 61096114,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "版本号",
            prop: "app_version",
            type: "input",
            search: true,
            placeholder: "列入: 1.0.0",
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
          {
            label: "用户群组",
            prop: "group_id",
            type: "select",
            search: true,
            placeholder: "用户属性",
            dicUrl: `${catUrl}/${getGroupList.getGroupList}`,
            props: {
              label: "group_name",
              value: "group_id",
            },
            dicFormatter: (res) => {
              return res.source;
            },
          },
           {
            label: "跳转页面",
            prop: "jump_id",
            type: "select",
            placeholder: "请选择跳转页面",
            dicData: [],
            props: {
              label: "title",
              value: "id",
            },
            rules: [
              {
                required: false,
                message: "跳转页面必须选择",
              },
            ],
            change: (value) => {
              let data = value.column.dicData.filter(
                (item) => item.id == value.value
              )[0];
              // debugger

              if (data != undefined) {
                // this.option.column[8].tip = data.info;
                setTimeout(() => {
                  this.form.info2 = data.info;
                  // this.form.jump_url = data.url;
                  this.option.column[5].value = data.info;
                  this.option.column[5].addDisplay = true;
                  this.option.column[5].editDisplay = true;
                }, 1);
              } else {
                this.form.info2 = "";
                // this.form.jump_url = "";
                this.option.column[5].value = "";
                this.option.column[5].addDisplay = false;
                this.option.column[5].editDisplay = false;
              }
            },
          },
          {
            label: "跳转地址",
            prop: "jump_url",
            type: "input",
            hide: true,
            tip: "",
            placeholder: "跳转地址",
          },
          {
            label: "",
            prop: "info2",
            span: 24,
            tip: "跳转地址说明",
            type: "textarea",
            placeholder: "无跳转地址说明",
            hide: true,
            value: "",
            addDisplay: false,
            editDisplay: false,
            readonly: true,
          },
          {
            type: "upload",
            label: "图标",
            span: 24,
            display: true,
            showFileList: true,
            multiple: false,
            limit: 10,
            propsHttp: {},
            canvasOption: {},
            prop: "img_url",
            drag: false,
            listType: "picture-img",
            action: `${catUrl}${uploadFile.uploadFile}`,
            propsHttp: {
              url: "source",
            },
            rules: [
              {
                required: true,
                message: "图片必须上传",
              },
            ],
          },
          {
            type: "textarea",
            label: "推荐语",
            prop: "info",
            hide: true,
            span: 24,
            rules: [
              {
                required: true,
                message: "推荐语不能为空",
              },
            ],
          },
         
          
          {
            label: "排序",
            prop: "sort",
            type: "number",
            addDisabled: true,
            search: false,
            rules: [
              { required: false, message: "排序必须填写" },
              { type: "number", message: "必须为数字值" },
            ],
          },
          {
            label: "开始时间",
            prop: "start_date",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            placeholder: "开始时间",
            rules: [
              {
                required: true,
                message: "开始时间必须填写",
              },
            ],
          },
          {
            label: "结束时间",
            prop: "end_date",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            placeholder: "结束时间",
            rules: [
              {
                required: true,
                message: "结束时间必须填写",
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
            slot: true,
          },
        ],
      },
    };
  },
  async created() {
    //查询跳转页面列表
    let { data } = await common(appPopup.getSkippaheType, null, null, "GET");
    this.option.column[3].dicData = data;
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.pageAndParam}`,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      if (row.start_time > row.end_time) {
        loading();
        return this.$message.error("开启时间不可大于结束时间");
      }

      //判断是否填写了跳转路径
      if (row.jump_id == 1 || row.jump_id == 2 || row.jump_id ==13) {
        if (row.jump_url == undefined || row.jump_url == "") {
          loading();
          return this.$message.error("请填写跳转路径(参数)");
        }
      } else {
        row.jump_url = "";
      }

      delete row["id"];
      row.jump_name = row["$jump_id"];
      row.create_time = new Date();

      row.position_id = 53;
      let userInfo = this.$store.state.user.userInfo;
      row.edit_account = userInfo.username;
      row.edit_time = new Date();
      //       scope.row.admin_id = userInfo.id ? userInfo.id : null
      //       scope.row.admin_name = userInfo.username
      let data = await common(`${baseURL.save}`, row, null, "POST");
      loading();
      if (data == null) {
        return;
      }

      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      if (row.start_time > row.end_time) {
        loading();
        return this.$message.error("开启时间不可大于结束时间");
      }

      //判断是否填写了跳转路径
      if (row.jump_id == 1 || row.jump_id == 2 || row.jump_id == 13) {
        if (row.jump_url == undefined || row.jump_url == "") {
          loading();
          return this.$message.error("请填写跳转路径(参数)");
        }
      } else {
        row.jump_url = "";
      }

      row.jump_name = row["$jump_id"];
      let userInfo = this.$store.state.user.userInfo;
      row.edit_account = userInfo.username;
      row.edit_time = new Date();
      row.position_id = 53;
      let data = await common(`${baseURL.update}`, row, null, "POST");
      loading();
      if (data == null) {
        return;
      }

      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            `${baseURL.delete}`,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      // params.title = `%${params.title ? params.title : ""}%`;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>